import {defineConfig} from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
import AutoImport from 'unplugin-auto-import/vite'
import discardComments from 'postcss-discard-comments';

export default defineConfig({
    server: {
        proxy: {
            '/api': {
                target: 'http://localhost:8000/api',
                changeOrigin: true,
                rewrite: (path) => path.replace(/^\/api/, '')
            },

            '/media': {
                target: 'http://localhost:8000/media',
                changeOrigin: true,
                rewrite: (path) => path.replace(/^\/media/, '')
            }
        }
    },
    define: {
        'import.meta.env.VITE_API_BASE_URL': JSON.stringify('https://api.example.com')
    },// 环境变量配置
    assetsInclude: ['**/*.eot'],// 打包时排除eot文件
    build: {
        outDir: './build',
        chunkSizeWarningLimit: 1000,
        rollupOptions: {
            external: ['wangEditor'], // 如果wangEditor是通过CDN引入
            output: {
                manualChunks(id) {
                    // 更灵活的手动分块方式
                    if (id.includes('node_modules/vue') || id.includes('node_modules/vue-router') || id.includes('node_modules/axios')) {
                        return 'vendor';
                    }
                    if (id.includes('node_modules/wangEditor')) {
                        return 'editor';
                    }
                },
                entryFileNames: `assets/[name].[hash].js`,
                chunkFileNames: `assets/[name].[hash].js`,
                assetFileNames: `assets/[name].[hash].[ext]`,
            },
        },
    },

    plugins: [
        vue(),
        AutoImport({
            imports: [
                'vue',
                'vue-router', // 自动导入vue-router相关API
                {
                    'axios': [
                        ['default', 'axios']// 自动导入axios并命名为axios
                    ]
                }
            ],
            dts: true, // 生成自动导入的TypeScript声明文件
            eslintrc: {
                enabled: true, // 生成eslint配置
            },
        })
    ],// 自动导入插件配置
    resolve: {
        alias: {
            '@': path.resolve(__dirname, './src'),
            '@stores': path.resolve(__dirname, './src/stores'),
        }
    },// 路径别名配置
})
